﻿var BaseProperty = {

    ActivityProperty: {
        activityName: {
            'title': "节点名称：",
            'control': $("<input id='activityName' type='text'>")
        },
        DESCRIPTION: {
            'title': "描述：",
            'control': $("<textarea id='DESCRIPTION'>")
        },
        //完成方式
        FINISH_TYPE: {
            'title': ["完成方式：", "合并方式："],
            'control': [$("<select style='width:120px; margin-right:0px;' ><option  value='0' >单一</option><option  value='1'>百分比</option><option value='2'>数量</option><option value='3'>全部</option></select>"),
            $("<input  disabled='disabled' style='width:25px;'>")
            ]
        },
        //选人方式
        IS_AUTOMATIC: {
            'title': "选人方式：",
            'control': $("<select ><option value='0'  >手工选人</option><option  value='1'>自动选人</option></select>")
        },
        //路由方式
        RouteType: {
            'title': "路由方式：",
            'control': $("<select ><option value='0'  >Up</option><option  value='1'>Down</option></select>")
        },

        //人工决策
        IS_ARTIFICIAL_DECISION: {
            'title': "人工决策：",
            'title1': '是否转批',
            'control': $("<select ><option value='1' >是</option><option  value='0'>否</option></select>")
        },

        //是否编辑表单
        ENABLE_EDIT_ATTACHMENT: {
            'title': "是否编辑表单",
            'control': $("  <input type='checkbox'> ")
        },

        //是否添加附件
        ENABLE_ATTACHMENT: {
            'title': "是否可以上传附件",
            'control': $("  <input type='checkbox'> ")
        },
        //是否查看审批历史
        ENABLE_SHOW: {
            'title': "是否查看审批历史",
            'control': $("  <input type='checkbox'> ")
        },
        //是否提交意见
        ENABLE_SUGGESTION: {
            'title': "是否提交意见",
            'control': $("  <input type='checkbox'> ")
        },

        //是否终止流程
        ENABLE_TERMINATE_FLOW: {
            'title': "是否终止流程",
            'control': $("  <input type='checkbox'> ")
        },
        //是否需要签名
        ENABLE_SIGNATURE: {
            'title': "是否需要签名",
            'control': $("  <input type='checkbox'> ")
        },
        IS_MERGR: {
            'title': "合并审批节点",
            'control': $("  <input type='checkbox'> ")
        },
        ENABLE_TRANSFER: {
            'title': "是否转批",
            'control': $("  <input type='checkbox'> ")
        },
        ENABLE_CIRCULATED: {
            'title': "是否传阅",
            'control': $("  <input type='checkbox'> ")
        },
        ENABLE_FILED: {
            'title': "是否归档",
            'control': $("  <input type='checkbox'> ")
        },
        OPEN_MODE_TYPE: {
            'title': "打开模式：",
            'control': $("<select >\
                <option  value='0'  >强制保留痕迹</option>\
                <option  value='1'>核稿模式</option>\
                <option  value='2'>普通编辑模式</option>\
                <option  value='3'>只读模式</option>\
                <option  value='4'>手写模式</option>\
                </select>")
        },
        IS_UPLOAD_AUTOMATIC: {
            'title': "是否限制上传附件",
            'control': $("  <input type='checkbox'> ")
        },


    },


    InitialPage: function (parent, activity) {
        var me = this;
        me.activity = activity;
        me.InitialControl(parent);
        me.InitialData(activity);

    },

    //加载数据
    InitialData: function () {
        var me = this;
        me.ActivityProperty.activityName.control.val(me.activity.ActivityName);
        if (me.activity.ActivityProperty != undefined) {
            me.ActivityProperty.activityName.control.val(me.activity.ActivityProperty.activityName);
            me.ActivityProperty.DESCRIPTION.control.val(me.activity.ActivityProperty.DESCRIPTION);
            me.ActivityProperty.FINISH_TYPE.control[0].val(me.activity.ActivityProperty.FINISH_TYPE);
            me.ActivityProperty.FINISH_TYPE.control[1].val(me.activity.ActivityProperty.finishNumber);

            if (me.activity.ActivityProperty.FINISH_TYPE == 1 || me.activity.ActivityProperty.FINISH_TYPE == 2) {
                me.ActivityProperty.FINISH_TYPE.control[1].removeAttr("disabled");
            }
            else {
                me.ActivityProperty.FINISH_TYPE.control[1].attr("disabled", "disabled");
                me.ActivityProperty.FINISH_TYPE.control[1].val('');
            }


            me.ActivityProperty.IS_AUTOMATIC.control.val(me.activity.ActivityProperty.IS_AUTOMATIC);

            me.ActivityProperty.OPEN_MODE_TYPE.control.val(me.activity.ActivityProperty.OPEN_MODE_TYPE);

            me.ActivityProperty.IS_ARTIFICIAL_DECISION.control.val(me.activity.ActivityProperty.IS_ARTIFICIAL_DECISION);


            if (me.activity.ActivityProperty.ENABLE_EDIT_ATTACHMENT == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_EDIT_ATTACHMENT.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_EDIT_ATTACHMENT.control);

            if (me.activity.ActivityProperty.ENABLE_ATTACHMENT == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_ATTACHMENT.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_ATTACHMENT.control);
            if (me.activity.ActivityProperty.IS_UPLOAD_AUTOMATIC == 1)
                me.SetCheck(me.ActivityProperty.IS_UPLOAD_AUTOMATIC.control);
            else
                me.SetUnCheck(me.ActivityProperty.IS_UPLOAD_AUTOMATIC.control);
            
            if (me.activity.ActivityProperty.ENABLE_SHOW == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_SHOW.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_SHOW.control)
            if (me.activity.ActivityProperty.ENABLE_SUGGESTION == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_SUGGESTION.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_SUGGESTION.control);
            //if (me.activity.ActivityProperty.ENABLE_TRANSIT == 1)
            //    me.SetCheck(me.ActivityProperty.ENABLE_TRANSIT.control);
            //else
            //    me.SetUnCheck(me.ActivityProperty.ENABLE_TRANSIT.control);
            if (me.activity.ActivityProperty.ENABLE_TERMINATE_FLOW == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_TERMINATE_FLOW.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_TERMINATE_FLOW.control);
            if (me.activity.ActivityProperty.ENABLE_SIGNATURE == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_SIGNATURE.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_SIGNATURE.control);
            if (me.activity.ActivityProperty.IS_MERGR == 1)
                me.SetCheck(me.ActivityProperty.IS_MERGR.control);
            else
                me.SetUnCheck(me.ActivityProperty.IS_MERGR.control);
            if (me.activity.ActivityProperty.ENABLE_TRANSFER == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_TRANSFER.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_TRANSFER.control);
            if (me.activity.ActivityProperty.ENABLE_CIRCULATED == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_CIRCULATED.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_CIRCULATED.control);
            if (me.activity.ActivityProperty.ENABLE_FILED == 1)
                me.SetCheck(me.ActivityProperty.ENABLE_FILED.control);
            else
                me.SetUnCheck(me.ActivityProperty.ENABLE_FILED.control);
        }
        else {
            me.ActivityProperty.DESCRIPTION.control.val('');
            me.ActivityProperty.FINISH_TYPE.control[0].val(0);
            me.ActivityProperty.FINISH_TYPE.control[1].attr("disabled", "disabled");
            me.ActivityProperty.FINISH_TYPE.control[1].val('');
            me.ActivityProperty.IS_AUTOMATIC.control.val(0);
            me.ActivityProperty.OPEN_MODE_TYPE.control.val(0);
            me.ActivityProperty.IS_ARTIFICIAL_DECISION.control.val(0);
            me.ActivityProperty.ENABLE_EDIT_ATTACHMENT.control.attr("checked", false);
            me.ActivityProperty.ENABLE_ATTACHMENT.control.attr("checked", false);
            me.ActivityProperty.IS_UPLOAD_AUTOMATIC.control.attr("checked", false);
            
            me.ActivityProperty.ENABLE_SHOW.control.attr("checked", false);
            me.ActivityProperty.ENABLE_SUGGESTION.control.attr("checked", false);
            //me.ActivityProperty.ENABLE_TRANSFER.control.attr("checked", false);
            me.ActivityProperty.ENABLE_TERMINATE_FLOW.control.attr("checked", false);
            me.ActivityProperty.ENABLE_SIGNATURE.control.attr("checked", false);
            me.ActivityProperty.IS_MERGR.control.attr("checked", false);
            me.ActivityProperty.ENABLE_TRANSFER.control.attr("checked", false);
            me.ActivityProperty.ENABLE_CIRCULATED.control.attr("checked", false);
            me.ActivityProperty.ENABLE_FILED.control.attr("checked", false);


        }

    },

    //初始化控件
    InitialControl: function (parent) {
        var me = this;
        var activityType = me.activity.activityType;
        var container = $("<div>");

        var div = $("<div class='Prop'>");
        container.append(div);
        div.html(me.ActivityProperty.activityName.title);
        div.append(me.ActivityProperty.activityName.control);
        div = $("<div class='Long'>");
        container.append(div);
        div.html(me.ActivityProperty.DESCRIPTION.title);
        div.append(me.ActivityProperty.DESCRIPTION.control);

        if (activityType == ActivityType.End) {

            div = "<div style='width:600px; height:25px; float:left; border-bottom:1px solid gray;  margin-left:20px;' >";
            container.append(div);
            div = $("<div class='checkBox'>");
            div.append(me.ActivityProperty.ENABLE_FILED.control);
            div.append(me.ActivityProperty.ENABLE_FILED.title);
            container.append(div);

        }
        if (activityType != ActivityType.Start && activityType != ActivityType.End) {

            if (activityType == ActivityType.Merging || activityType == ActivityType.Approval || activityType == ActivityType.Freedom) {
                div = $("<div class='Prop'>");
                container.append(div);

                if (activityType == ActivityType.Merging)
                    div.html(me.ActivityProperty.FINISH_TYPE.title[1]);
                else
                    div.html(me.ActivityProperty.FINISH_TYPE.title[0]);
                div.append(me.ActivityProperty.FINISH_TYPE.control[0]);
                div.append(me.ActivityProperty.FINISH_TYPE.control[1]);
                me.ActivityProperty.FINISH_TYPE.control[0].change(function () {
                    var selectedvalue = me.ActivityProperty.FINISH_TYPE.control[0].find("option:selected").val();
                    if (selectedvalue == 1 || selectedvalue == 2) {
                        me.ActivityProperty.FINISH_TYPE.control[1].removeAttr("disabled");
                    }
                    else {
                        me.ActivityProperty.FINISH_TYPE.control[1].attr("disabled", "disabled");
                        me.ActivityProperty.FINISH_TYPE.control[1].val('');
                    }
                    //if (selectedvalue > 0) {
                    //    if (activityType == ActivityType.Approval || activityType == ActivityType.Freedom) {
                    //        div1.css('display', 'block');
                    //        div2.css('display', 'none');

                    //    }
                    //}
                    //else {
                    //    if (activityType == ActivityType.Approval || activityType == ActivityType.Freedom) {
                    //        div1.css('display', 'none');
                    //        div2.css('display', 'block');
                    //    }

                    //}
                })
                div = $("<div class='Prop'>");
                container.append(div);
                div.html(me.ActivityProperty.IS_AUTOMATIC.title);
                div.append(me.ActivityProperty.IS_AUTOMATIC.control);

            }
            //var div1 = $("<div class='Prop'>");
            //container.append(div1);
            //div1.html(me.ActivityProperty.RouteType.title);
            //div1.append(me.ActivityProperty.RouteType.control);


            if (activityType == ActivityType.Condition || activityType == ActivityType.Branching) {
                div = $("<div class='Prop'>");
                container.append(div);
                div.html(me.ActivityProperty.IS_ARTIFICIAL_DECISION.title);
                div.append(me.ActivityProperty.IS_ARTIFICIAL_DECISION.control);
            } else {
                div = $("<div class='Prop'>");
                container.append(div);
                div.html(me.ActivityProperty.OPEN_MODE_TYPE.title);
                div.append(me.ActivityProperty.OPEN_MODE_TYPE.control);


                div = $("<div class='checkBox'>");
                div.append(me.ActivityProperty.IS_UPLOAD_AUTOMATIC.control);
                div.append(me.ActivityProperty.IS_UPLOAD_AUTOMATIC.title);
                container.append(div);

            }

            if (activityType == ActivityType.Approval || activityType == ActivityType.Freedom) {
                //分割线
                div = "<div style='width:600px; height:25px; float:left; border-bottom:1px solid gray;  margin-left:20px;' >";
                container.append(div);




                div = $("<div class='checkBox'>");
                div.append(me.ActivityProperty.ENABLE_EDIT_ATTACHMENT.control);
                div.append(me.ActivityProperty.ENABLE_EDIT_ATTACHMENT.title);
                container.append(div);

                div = $("<div class='checkBox'>");
                div.append(me.ActivityProperty.ENABLE_ATTACHMENT.control);
                div.append(me.ActivityProperty.ENABLE_ATTACHMENT.title);
                container.append(div);

           
                

                div = $("<div class='checkBox'>");
                div.append(me.ActivityProperty.ENABLE_SHOW.control);
                div.append(me.ActivityProperty.ENABLE_SHOW.title);
                container.append(div);

                div = $("<div class='checkBox'>");
                div.append(me.ActivityProperty.ENABLE_SUGGESTION.control);
                div.append(me.ActivityProperty.ENABLE_SUGGESTION.title);
                container.append(div);

                div = $("<div class='checkBox'>");
                div.append(me.ActivityProperty.ENABLE_TERMINATE_FLOW.control);
                div.append(me.ActivityProperty.ENABLE_TERMINATE_FLOW.title);
                container.append(div);


                div = $("<div class='checkBox'>");
                div.append(me.ActivityProperty.ENABLE_SIGNATURE.control);
                div.append(me.ActivityProperty.ENABLE_SIGNATURE.title);
                container.append(div);

                if (activityType == ActivityType.Approval) {



                    div = $("<div class='checkBox'>");
                    div.append(me.ActivityProperty.ENABLE_TRANSFER.control);
                    div.append(me.ActivityProperty.ENABLE_TRANSFER.title);
                    container.append(div);

                    div = $("<div class='checkBox'>");
                    div.append(me.ActivityProperty.ENABLE_CIRCULATED.control);
                    div.append(me.ActivityProperty.ENABLE_CIRCULATED.title);
                    container.append(div);




                }


                if (activityType == ActivityType.Approval) {
                    div = $("<div class='checkBox'>");
                    div.append(me.ActivityProperty.IS_MERGR.control);
                    div.append(me.ActivityProperty.IS_MERGR.title);
                }
                container.append(div);
            }
        }
        parent.append(container);
    },

    //保存数据
    SaveData: function (activity) {
        var me = this;
        if (activity.ActivityProperty == undefined)
            activity.ActivityProperty = {};
        activity.ActivityProperty.activityName = me.ActivityProperty.activityName.control.val();
        activity.ActivityProperty.DESCRIPTION = me.ActivityProperty.DESCRIPTION.control.val();
        activity.ActivityProperty.FINISH_TYPE = me.ActivityProperty.FINISH_TYPE.control[0].find("option:selected").val();
        activity.ActivityProperty.finishNumber = me.ActivityProperty.FINISH_TYPE.control[1].val();
        activity.ActivityProperty.IS_AUTOMATIC = me.ActivityProperty.IS_AUTOMATIC.control.find("option:selected").val();
        activity.ActivityProperty.OPEN_MODE_TYPE = me.ActivityProperty.OPEN_MODE_TYPE.control.find("option:selected").val();

        
        activity.ActivityProperty.IS_ARTIFICIAL_DECISION = me.ActivityProperty.IS_ARTIFICIAL_DECISION.control.find("option:selected").val();
        activity.ActivityProperty.ENABLE_EDIT_ATTACHMENT = me.ActivityProperty.ENABLE_EDIT_ATTACHMENT.control.is(":checked") ? 1 : 0;
        
        activity.ActivityProperty.ENABLE_ATTACHMENT = me.ActivityProperty.ENABLE_ATTACHMENT.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.IS_UPLOAD_AUTOMATIC = me.ActivityProperty.IS_UPLOAD_AUTOMATIC.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.ENABLE_SHOW = me.ActivityProperty.ENABLE_SHOW.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.ENABLE_SUGGESTION = me.ActivityProperty.ENABLE_SUGGESTION.control.is(":checked") ? 1 : 0;
        //activity.ActivityProperty.ENABLE_TRANSIT = me.ActivityProperty.ENABLE_TRANSFER.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.ENABLE_TERMINATE_FLOW = me.ActivityProperty.ENABLE_TERMINATE_FLOW.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.ENABLE_SIGNATURE = me.ActivityProperty.ENABLE_SIGNATURE.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.IS_MERGR = me.ActivityProperty.IS_MERGR.control.is(":checked") ? 1 : 0;




        activity.ActivityName = me.activity.ActivityProperty.activityName;


        activity.ActivityProperty.ENABLE_TRANSFER = me.ActivityProperty.ENABLE_TRANSFER.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.ENABLE_CIRCULATED = me.ActivityProperty.ENABLE_CIRCULATED.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.ENABLE_FILED = me.ActivityProperty.ENABLE_FILED.control.is(":checked") ? 1 : 0;
        activity.ActivityProperty.status = 0;
        for (i in ActivityPropertyStatus) {
            var option = ActivityPropertyStatus[i];
            var op = option.split('|');
            op[0] = parseInt(op[0]);
            op[1] = parseInt(op[1]);
            var tmp = (activity.ActivityProperty[i] << op[0]);
            activity.ActivityProperty.status |= tmp;
        }

    },

    //设置选择框被选中
    SetCheck: function (check) {
        if (!check.is(":checked"))
            check.click();
    },
    //取消选择框选中
    SetUnCheck: function (check) {
        check.attr("checked", false);
    }

}